//
//  SheetCell.m
//  IBDP
//  联动表格
//  Created by LYPC on 2017/7/24.
//  Copyright © 2017年 cattsoft. All rights reserved.
//

#import "SheetCell.h"

@interface SheetCell ()

@property (nonatomic, strong) NSMutableArray *itemLabels; // 每行的label

@property (nonatomic,strong) NSArray *contents; // 每行的数据
@property (nonatomic,strong) UILabel *markLabel;

@end

@implementation SheetCell


+ (instancetype)cellWithTableView:(UITableView *)tableView cellContents:(NSArray *)contents type:(NSString *)type {
    static NSString *ReuseID = @"SheetCell";
    SheetCell *cell = [tableView dequeueReusableCellWithIdentifier:ReuseID];
    if (!cell) {
        cell = [[SheetCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ReuseID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.contentView.backgroundColor = [UIColor blackColor];
    }
    [cell setupUIData:contents type:type];
    return cell;
}

- (void)setupUIData:(NSArray *)contents type:(NSString *)type {
    self.contents = contents;
    [self.itemLabels enumerateObjectsUsingBlock:^(UILabel *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        obj.text = contents[idx];
        if (idx == 0 && [type isEqualToString:@"L"]) {
            self.markLabel.text = @"1";
        }
    }];
}

- (NSMutableArray *)itemLabels {
    if (!_itemLabels) {
        _itemLabels = [NSMutableArray array];
        [self.contents enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(idx*ItemW, 0, ItemW, ItemH)];
            label.textColor = [UIColor whiteColor];
            label.font = [UIFont systemFontOfSize:Font];
            label.textAlignment = NSTextAlignmentCenter;
            label.text = self.contents[idx];
            label.numberOfLines = 0;
            label.layer.masksToBounds = YES;
            label.layer.borderColor = [UIColor blueColor].CGColor;
            label.layer.borderWidth = 0.5;
            [self.contentView addSubview:label];
            if (idx == 0) {
                self.markLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 15, 15)];
                self.markLabel.textColor = [UIColor whiteColor];
                self.markLabel.font = [UIFont systemFontOfSize:Font-2];
                self.markLabel.textAlignment = NSTextAlignmentCenter;
                [label addSubview:self.markLabel];
            }
            [_itemLabels addObject:label];
        }];
    }
    return _itemLabels;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
